﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxNavBar;
using DMS.Web.Messages;

namespace DMS.Web
{
	public partial class MasterAjax : System.Web.UI.MasterPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			MessageManager.SetMessage(null);

			if (ASPxSiteMapDataSource1.Provider != null)
			{
				SiteMapNode node = ASPxSiteMapDataSource1.Provider.CurrentNode;
				if (node != null)
				{
					lblCurrent.Text= node.Description;
				}
			}
		}

		/* Skins */
		protected void cbSkins_DataBound(object sender, EventArgs e)
		{
			if (IsPostBack) return;

			ListEditItem item = cbSkins.Items.FindByValue(Page.Theme) ?? cbSkins.Items.FindByValue("Glass");
			if (item != null)
				cbSkins.SelectedItem = item;
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			UpdateMessagePanel();
		}

		#region Messages

		private void UpdateMessagePanel()
		{
			if (MessageManager.CurrentMessage==null)
			{
				pnlMessage.Visible = false;
				return;
			}
			pnlMessage.Visible = true;

			MessageSeverity maxSeverity = MessageManager.CurrentMessage.Severity;
			SetMessageColors(pnlMessage, maxSeverity);

			lblMessages.Text = MessageManager.CurrentMessage.Text;
		}

		private static void SetMessageColors(WebControl control, MessageSeverity severity)
		{
			if (severity.Equals(MessageSeverity.Error))
			{
				control.BackColor = Color.Red;
			}
			else if (severity.Equals(MessageSeverity.Warning))
			{
				control.BackColor = Color.YellowGreen;
			}
			else if (severity.Equals(MessageSeverity.Notice))
			{
				control.BackColor = Color.LightSalmon;
			}
		}

		#endregion
	}
}
